package com.jiantong.work.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import io.terminus.common.utils.JsonMapper;
import lombok.Data;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by niufei on 2017/3/21.
 * 屠宰申报
 */
@Data
public class ButcherReport implements Serializable{

    private static final ObjectMapper objectMapper = JsonMapper.nonEmptyMapper().getMapper();

    private static final long serialVersionUID = -5472072091563159645L;

    /**
     * 主键id
     */
    private Long id;

    /**
     * 申报编号
     */
    private String number;

    /**
     * 申报时间
     */
    private Date date;

    /**
     * 入场监督检疫ID
     */
    private Long admissionInspectionId;

    /**
     * 入场监督检疫编号
     */
    private String admissionInspectionNumber;

    /**
     * 屠宰场id
     */
    private Long butcherFactoryId;

    /**
     * 屠宰场名称
     */
    private String butcherFactoryName;

    /**
     * 申报人ID
     */
    private Long userId;

    /**
     * 申报人姓名
     */
    private String userName;

    /**
     * 申报人电话
     */
    private String userMobile;

    /**
     * 状态 -2:已处理 -1：驳回 0：未提交 1：待处理 2：宰前检查 3：同步检疫 4：合格发证 ：5 发证
     */
    private Integer status;

    /**
     * 检疫合格证编号
     */
    private String quarantineCertNumber;

    /**
     * 不存数据库
     */
    private Map<String , Map<String , String>> quarantineCertPhotoMap;

    /**
     * 检疫合格证照片
     */
    @JsonIgnore
    private String quarantineCertPhoto;

    /**
     * 产品类型：牛、马、猪...
     */
    private Integer productType;

    /**
     * 单位
     */
    private Integer unit;

    /**
     * 数量
     */
    private Integer quantity;

    /**
     * 禽兽耳标号
     */
    @JsonIgnore
    private String earTagNumber;


    /**
     * 耳标号list
     */
    private List<String> earTagNumbers;



    /**
     * 货主 -- 待定
     */
    private String owner;

    /**
     * 货主电话
     */
    private String ownerMobile;

    /**
     * 来源id 养殖场id
     */
    private Long sourceId;

    /**
     * 来源name
     */
    private String sourceName;

    /**
     * 是否本地--待定
     */
    private Integer isLocal;

    /**
     * 单据标志位 1：正常，-1：删除
     */
    private Integer deleteFlag;

    /**
     * 创建时间
     */
    private Date createdAt;

    /**
     * 更新时间
     */
    private Date updatedAt;


    public void setQuarantineCertPhoto(String quarantineCertPhoto){
        this.quarantineCertPhoto = quarantineCertPhoto;
        if(Strings.isNullOrEmpty(quarantineCertPhoto)){
            this.quarantineCertPhotoMap = Maps.newHashMap();;
        }else {
            try {
                this.quarantineCertPhotoMap = objectMapper.readValue(quarantineCertPhoto, new TypeReference<Map<String , Map<String , String>>>(){});
            } catch (IOException e) {
                //ignore this fuck IOException
            }
        }
    }

    public void setQuarantineCertPhotoMap(Map<String , Map<String , String>> quarantineCertPhotoMap){
        this.quarantineCertPhotoMap = quarantineCertPhotoMap;
        if(quarantineCertPhotoMap == null || quarantineCertPhotoMap.isEmpty()){
            this.quarantineCertPhoto = null;
        }else {
            try {
                this.quarantineCertPhoto = objectMapper.writeValueAsString(quarantineCertPhotoMap);
            } catch (JsonProcessingException e) {
                //ignore this fuck IOException
            }
        }
    }


    public void setEarTagNumber(String earTagNumber){
        this.earTagNumber = earTagNumber;

        if(Strings.isNullOrEmpty(earTagNumber)){
            this.earTagNumbers = new ArrayList<>();
        }else {
            try {
                this.earTagNumbers =
                        objectMapper.readValue(earTagNumber, new TypeReference<List<String>>() {});
            } catch (IOException e) {
                //this is fuck exception
            }
        }
    }


    public void setEarTagNumbers(List<String> earTagNumbers){
        this.earTagNumbers = earTagNumbers;

        if(earTagNumbers == null || earTagNumbers.isEmpty()){
            this.earTagNumber = null;
        }else {
            try {
                this.earTagNumber =
                        objectMapper.writeValueAsString(earTagNumbers);
            } catch (JsonProcessingException e) {
                //this is fuck exception
            }
        }
    }


}
